package com.cb.domain.service.file;

import java.text.DecimalFormat;
import java.util.Calendar;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.safehaus.uuid.UUIDGenerator;


/**
 * 图片上传抽象类
 * @author sdm
 *
 */
public abstract class AbstractFileUpload implements FileUpload{
	
	protected UUIDGenerator generator  =  UUIDGenerator.getInstance();
	
	protected static Log logger=LogFactory.getLog(AbstractFileUpload.class);
	
	//文件大小限制
	protected Long fileSizeLimited=MAX_FILE_SIZE;
	//文件类型
	protected Integer fileType=FILE_TYPE_FILE;
	//允许的文件的扩展名
	protected String fileExtAllowed=ALLOWED_FILE_EXT_ALL;
	
	protected Long[] fileSize;
	

	public String getFileExtAllowed() {
		return fileExtAllowed;
	}

	public void setFileExtAllowed(String fileExtAllowed) {
		this.fileExtAllowed = fileExtAllowed;
	}

	public void setFileSizeLimited(Long fileSizeLimited){
		this.fileSizeLimited=fileSizeLimited;
	}
	
	public Long getFileSizeLimited() {
		return fileSizeLimited;
	}

	public Integer getFileType() {
		return fileType;
	}

	public void setFileType(Integer fileType) {
		this.fileType = fileType;
	}

	/**
	 * 取得文件的目录
	 * @return
	 */
	public String getFileDirectoryPath(){
		String dir="attachments";
		if(fileType<20){//图片
			dir+="/imgs";
			if(FILE_TYPE_IMAGE_Ad_IMAGE.equals(fileType)){
				dir+="/ad";
			}else if (FILE_TYPE_IMAGE_PRODUCT.equals(fileType)){
				dir+="/product";
			}else if(FILE_TYPE_IMAGE_PHOTO_ALBUM.equals(fileType)){
				dir+="/userAlbum";
			}else if(FILE_TYPE_IMAGE_PROMOTIONS.equals(fileType)){
				dir+="/promotions";
			}else if(FILE_ACTIVITY_AWARDS.equals(fileType)){
				dir+="/activity";
			}else if(FILE_TYPE_IMAGE_ACTIVITY_THEME.equals(fileType)){
				dir+="/activityTheme";
			}else{
				dir+="/others";
			}
		}else{
			dir+="/files";
		}
		 
		Calendar c = Calendar.getInstance();
		String monthStr="";

		int yyyy, mm;
		yyyy = c.get(Calendar.YEAR);
		mm = c.get(Calendar.MONTH) + 1;
		if(mm<10){
			monthStr="0";
		}
		monthStr+=String.valueOf(mm);
		dir+="/"+yyyy+monthStr;
		return dir;
	}
	
	/**
	 * 产生文件名
	 * @return
	 */
	protected String getGenernateFileName(){
		 Double du =  generator.getRandomNumberGenerator().nextDouble();
	     DecimalFormat df=new DecimalFormat("000000000000000000");
	     String fileName=df.format(du* new Double("100000000000000000"));
	     
	     fileName+=IMG_SUFFIX_ORG;	
	     
	     return fileName;
	}


	public Long[] getFileSize() {
		return fileSize;
	}



}
